﻿/*****************************************************************************
 
 Use of Ext is under the Ext JS license, for more information, 
 please visit http://extjs.com/products/license.php page. 
  
 You should confirm that you have read, understood and accept ethical criteria listed. 
 If not, see <http://www.mvcnext.com/license/>.
  
 Version: 1.0.0.0
 Website: http://www.mvcnext.com/
 
 Copyright (c) Yoganand R. All rights reserved.
 For continued development and updates, visit http://yogsbreath.net
 
 ******************************************************************************/
using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.IO;
using System.Reflection;
using System.Collections;
using System.Web.UI;
using System.Text;

namespace MVCnExt.Web.UI.Ext
{
    public class ExtRecord : ExtConfig
    {

        public override IList<ScriptDescriptor> GetScriptDescriptors()
        {
            IList<ScriptDescriptor> list = base.GetScriptDescriptors();

            if (list.Count == 0)
                list.Add(new ExtScriptControlDescriptor("Ext.grid.ColumnModel", "ctl00"));

            ScriptControlDescriptorBase desc = (ScriptControlDescriptorBase)list[0];

            desc.Type = "Ext.grid.ColumnModel";

            String[] properties = new String[]{
                "name", "mapping", "type", "sortType", "sortDir", "dateFormat", "defaultValue"
            };

            Type component = typeof(ExtRecord);

            foreach (String property in properties)
            {
                PropertyInfo i = component.GetProperty(property);

                if (i == null)
                    throw new InvalidOperationException(String.Format("Property not found, 'ExtRecord.{0}'", property));

                Object value = i.GetValue(this, null);

                if (value == null)
                {
                    continue;
                }

                if (!(value is String) && !(value is recordType))
                    desc.AddPropertyBase(property, value);
                else if (value is recordType)
                    desc.AddPropertyBase(property, String.Format("'{0}'",value.ToString().ToLower()));
                else
                    desc.AddPropertyBase(property, String.Format("'{0}'", value));
            }

            return list;
        }

        #region Public Properties

        /// <summary>
        /// The name by which the field is referenced within the Record.
        /// This is referenced by, for example the DataIndex property in
        /// column definition objects passed to ColumnModel
        /// </summary>
        [Description("The name by which the field is referenced within the Record. This is referenced by, for example the DataIndex property in column definition objects passed to ColumnModel")]
        public String name { get; set; }

        /// <summary>
        /// (Optional) A path specification for use by the Reader implementation
        /// that is creating the Record to access the data value from the data object.
        /// 
        /// If an JsonReader is being used, then this is a string containing the javascript
        /// expression to reference the data relative to the Record item's root.
        /// 
        /// If an XmlReader is being used, this is an Ext.DomQuery path to the data item
        /// relative to the Record element.
        /// 
        /// If the mapping expression is the same as the field name, this may be omitted.
        /// </summary>
        [Description("(Optional) A path specification for use by the Reader implementation that is creating the Record to access the data value from the data object. If an JsonReader is being used, then this is a string containing the javascript expression to reference the data relative to the Record item's root. If an XmlReader is being used, this is an Ext.DomQuery path to the data item relative to the Record element. If the mapping expression is the same as the field name, this may be omitted.")]
        public String mapping { get; set; }

        /// <summary>
        /// The data type for conversion to displayable value
        /// </summary>
        [DefaultValue(recordType.Auto)]
        [Description("The data type for conversion to displayable value")]
        public recordType type { get; set; }

        /// <summary>
        /// Sort method
        /// 
        /// If use SortTypeMethod.Custom then need define CustomSortFunction
        /// </summary>        
        [Description("Sort method")]
        public sortType? sortType { get; set; }

        /// <summary>
        /// (Optional) Initial direction to sort
        /// </summary>
        [Description("(Optional) Initial direction to sort")]
        public sortDirection? sortDir { get; set; }

        /// <summary>
        /// (Optional) A format String for the Date.parseDate function
        /// </summary>
        [Description("(Optional) A format String for the Date.parseDate function")]
        public String dateFormat { get; set; }

        /// <summary>
        /// (Optional) The default value passed to the Reader when the field does not exist in the data object
        /// 
        /// Please pay attention that if you use string const then need wrap like this
        ///     DefaultValue="'String const'"
        /// </summary>
        [Description("(Optional) The default value passed to the Reader when the field does not exist in the data object")]
        public String defaultValue { get; set; }       

        #endregion
    }
}
